package AkkaDemos

import akka.actor.{Actor, ActorSystem, Props}
import com.typesafe.config.ConfigFactory

/**
  * tcp编程demo ,使用akka框架
  */
object TcpExample {
  def main(args: Array[String]): Unit = {
    //主程序入口
    val (clientHost, clientPort, serverHost, serverPort) = ("127.0.0.1", 7777, "127.0.0.1", 9999)
    val conf = ConfigFactory.parseString(
      s"""|akka.actor.provider="akka.remote.RemoteActorRefProvider"
          |akka.remote.netty.tcp.hostname=$serverHost
          |akka.remote.netty.tcp.port=$serverPort
      """.stripMargin
    )
    val server = ActorSystem("server", conf)
    //创建YellowChicker的actor和返回actorRef
    val ycs = server.actorOf(Props[YellowChickenServer], "ycs")
    ycs ! "start"

  }

  class YellowChickenServer extends Actor {
    override def receive: Receive = {
      case "start" => println("start, 小黄鸡开始工作了")
      case clientMessage(mes) => mes match {
        case "大数据学费" => {
          sender() ! serverMessage("学费30000RMB")
        }
        case "学校地址" => sender() ! serverMessage("北京仓平**路**大楼")
        case "学习什么技术" => sender() ! serverMessage("大数据, 前端, 区块链")
        case _ => sender() ! serverMessage("你说的是撒子????")
      }
    }
  }

}
